package com.sky.controller.admin;

import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.JstlUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

@Api(tags = "套餐相关接口")
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result getSetmealById(@PathVariable Long id){
        log.info("套餐id："+ id);
        SetmealVO setmealById = setmealService.getSetmealById(id);
        log.info("根据id查询套餐:"+setmealById);
        return Result.success(setmealById);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页参数："+setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 新增套餐
     * @param setmealVO
     * @return
     */
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealVO.categoryId")
    @PostMapping
    public Result insertSetmeal(@RequestBody SetmealVO setmealVO ){
        setmealService.insertSetmeal( setmealVO );
        return Result.success();
    }


    /**
     * 修改套餐状态
     * @param status
     * @param id
     * @return
     */
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @ApiOperation("修改套餐状态")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,@RequestParam Long id ){
        log.info("状态："+ status);
        log.info("套餐id："+id);

        return setmealService.updateStatus( status, id );
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealVO.categoryId")
    @ApiOperation("批量删除套餐")
    @DeleteMapping
    public Result deleteBatch(@RequestParam List<Long> ids){
        log.info("批量删除套餐参数：" +ids);
        setmealService.deleteBatch( ids);

        return setmealService.deleteBatch( ids);
    }



    //修改套餐

    /**
     * 修改套餐
     * @return
     */
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealVO.categoryId")
    @PutMapping
    public Result updateSetmeal(@RequestBody SetmealVO setmealVO){
        log.info("修改后的参数："+ setmealVO);
        setmealService.updateSetmeal( setmealVO);
        return Result.success();
    }




}
